package com.ruike.alisurface.Serials;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;

import com.ruike.alisurface.BuildConfig;
import com.ruike.alisurface.utils.MyCountDownTimer;
import com.ruike.alisurface.utils.SerialsVerify;
import com.voodoo.lib_utils.ByteUtils;
import com.voodoo.lib_utils.L;

import java.math.BigInteger;

public class ICCardUtils {

    private static final String TAG = "ICCardUtils";

    public static void initListener() {
        Ttys1Utils.getInstance().setOnSerialPortResultDataListener((resultDataStr, resultBytes) -> {
            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(() -> {
                if (resultBytes[0] == (byte) 0xAA && resultBytes[1] == (byte) 0xBB && resultBytes.length > 9) {
                    byte resultByte = resultBytes[8];
                    switch (resultByte) {
                        case 0x0A: // 通用错误
                            L.eTag(TAG, "通用错误：" + resultDataStr);
                            if (onReadCardListener != null) {
                                onReadCardListener.onResultCardNumber("", "刷卡异常，请重新进入刷卡界面");
                            }
                            break;
                        case 0x0E: // 射频基站损坏
                            L.eTag(TAG, "射频基站损坏：" + resultDataStr);
                            if (onReadCardListener != null) {
                                onReadCardListener.onResultCardNumber("", "射频基站损坏，请联系管理员");
                            }
                            break;
                    }
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    int lenth = resultBytes.length > 4 ? 10 : resultBytes.length;
                    for (int i = 0; i < lenth; i++) {
                        stringBuilder.append((char) (resultBytes[i]));
                    }
                    L.iTag(TAG, "获取到的卡号：" + stringBuilder.toString());
                    if (onReadCardListener != null) {
                        onReadCardListener.onResultCardNumber(stringBuilder.toString(), "获取到的卡号：" + stringBuilder.toString());
                    }
                }
            });
        });
    }

    public static OnReadCardListener onReadCardListener;

    public static void setOnICCardGetPwdListener(OnReadCardListener onReadCardListener) {
        ICCardUtils.onReadCardListener = onReadCardListener;
    }

    /**
     * 读卡返回监听
     */
    public interface OnReadCardListener {
        /**
         * 读取卡号回调
         *
         * @param cardNumber 读取到的卡号
         * @param errorMsg   异常信息
         */
        void onResultCardNumber(String cardNumber, String errorMsg);
    }

    // ==================================== M系列读卡器通讯协议 ====================================

    /**
     * 设置读写模式
     * <p>
     * 发送：AA BB 06 00 00 00 01 06 '01' 06 ----- 01进入读写模块（被动读写卡，由指令控制）  01  02
     * 返回：AA BB 06 00 00 00 01 06 00 07
     * <p>
     */
    public static void readWriteMode(boolean isZiDong) {
        byte[] strSerialComSend = new byte[10];
        strSerialComSend[0] = (byte) 0xAA;
        strSerialComSend[1] = (byte) 0xBB;
        strSerialComSend[2] = 0x06;
        strSerialComSend[3] = 0x00;
        strSerialComSend[4] = 0x00;
        strSerialComSend[5] = 0x00;
        strSerialComSend[6] = 0x01;
        strSerialComSend[7] = 0x06;
        strSerialComSend[8] = (byte) (isZiDong ? 2 : 1);
        strSerialComSend[9] = (byte) Integer.parseInt(SerialsVerify.getBCC(strSerialComSend, 4), 16);
        L.iTag(TAG, "发送读写模式指令：" + ByteUtils.byteArrToHex(strSerialComSend));
        Ttys1Utils.getInstance().SendPort(ByteUtils.byteArrToHex(strSerialComSend));
    }

}
